package com.buildings.activity.table;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.buildings.R;
import com.buildings.api.Api;
import com.buildings.api.Constant;
import com.buildings.base.BaseActivity;
import com.buildings.base.BaseApplication;
import com.buildings.base.JsonBean;
import com.buildings.bean.FaultTypeBean;
import com.buildings.bean.GeoGeomorphySvyPointDbEntity;
import com.buildings.bean.GeoGeomorphySvyPointEntity;
import com.buildings.bean.GeologicalSvyPlanningLineDbEntity;
import com.buildings.bean.LoginBean;
import com.buildings.http.BaseCallBack;
import com.buildings.http.JsonBuilder;
import com.buildings.http.MyHttpClient;
import com.buildings.util.CustomToast;
import com.buildings.util.DialogUtils;
import com.buildings.util.HYDialogUtils;
import com.buildings.util.HYImageUtils;
import com.buildings.util.PhotoRecycleBuild;
import com.buildings.util.SPUtil;
import com.buildings.util.StringUtil;
import com.buildings.util.TabTitleBuilder;
import com.buildings.view.ShowBottomDialog;
import com.google.gson.Gson;
import com.zr.earthquate.db.greendao.DaoSession;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

//地质地貌调查观测点-点
public class GeoGeomorphySvyPointActivity extends BaseActivity {
    @BindView(R.id.ll_tab_title)
    LinearLayout llTabTitle;
    @BindView(R.id.rv_pics)
    RecyclerView rvPics;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right_text)
    TextView tvRightText;
    @BindView(R.id.ll_visibile)
    LinearLayout llVisibile;

    @BindView(R.id.et_objectCode)
    EditText etObjectCode;
    @BindView(R.id.et_geologicalsvypointid)
    EditText etGeologicalsvypointid;
    @BindView(R.id.et_fieldid)
    EditText etFieldid;
    @BindView(R.id.et_geomorphycode)
    EditText etGeomorphycode;
    @BindView(R.id.et_geomorphyname)
    EditText etGeomorphyname;
    @BindView(R.id.et_geomorphytype)
    EditText etGeomorphytype;
    @BindView(R.id.et_verticaldisplacement)
    EditText etVerticaldisplacement;
    @BindView(R.id.et_horizenoffset)
    EditText etHorizenoffset;
    @BindView(R.id.et_geomorphylndirection)
    EditText etGeomorphylndirection;
    @BindView(R.id.tv_createdate)
    TextView tvCreatedate;
    @BindView(R.id.tv_issurfacerupturebelt)
    TextView tvIssurfacerupturebelt;
    @BindView(R.id.tv_fracturetype)
    TextView tvFracturetype;
    @BindView(R.id.et_photoAiid)
    EditText etPhotoAiid;
    @BindView(R.id.tv_photoviewingto)
    TextView tvPhotoviewingto;
    @BindView(R.id.et_photographer)
    EditText etPhotographer;
    @BindView(R.id.et_length)
    EditText etLength;
    @BindView(R.id.tv_width)
    EditText tvWidth;
    @BindView(R.id.et_height)
    EditText etHeight;
    @BindView(R.id.et_tensiondisplacement)
    EditText etTensiondisplacement;
    @BindView(R.id.et_feature)
    EditText etFeature;
    @BindView(R.id.tv_ismodifyworkmap)
    TextView tvIsmodifyworkmap;
    @BindView(R.id.tv_isinmap)
    TextView tvIsinmap;
    @BindView(R.id.et_lastangle)
    EditText etLastangle;
    @BindView(R.id.et_commentInfo)
    EditText etCommentInfo;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.tv_visibile)
    TextView tvVisibile;

    private TabTitleBuilder tabTitleBuilder;
    private PhotoRecycleBuild photoRecycleBuild;
    private Dialog myDialog;

    private DaoSession daoSession;
    private String urlArr;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geogeomorphysvypoint);
        ButterKnife.bind(this);
        daoSession = BaseApplication.getInstance().getDaoSession();
        tabTitleBuilder = new TabTitleBuilder(llTabTitle, this);
        initView();
    }

    private String type;
    private String taskId = "";
    private String projectId = "";
    private String projectName = "";
    private String taskName = "";
    private boolean isOnline;
    private int joninType;
    private String uuid;
    private long _id = -1;

    public void initView() {
        joninType = getIntent().getIntExtra("joninType", 1);
        photoRecycleBuild = new PhotoRecycleBuild(this, rvPics, joninType);
        if (joninType != 2) {
            tvTitle.setText("地质地貌调查观测点-点");
            tvRightText.setText("保存");
            double x = getIntent().getDoubleExtra("x", 0.0);
            double y = getIntent().getDoubleExtra("y", 0.0);
            type = getIntent().getStringExtra("type");
            taskId = getIntent().getStringExtra("taskId");
            projectId = getIntent().getStringExtra("projectId");
            projectName = getIntent().getStringExtra("projectName");
            taskName = getIntent().getStringExtra("taskName");
            isOnline = getIntent().getBooleanExtra("isOnLine", true);
            if (TextUtils.isEmpty(type)) {
                finish();
            }
            if (isOnline && TextUtils.isEmpty(taskId)){
                finish();
            }
            if (isOnline && TextUtils.isEmpty(projectId)){
                finish();
            }
            tabTitleBuilder.setLat(x);
            tabTitleBuilder.setLon(y);
            if (getIntent().hasExtra("_id")) {
                _id = getIntent().getLongExtra("_id", -1);
                setLocalData(_id);
            }
            if (1 == Constant.IS_ON_LINE){
                btnSubmit.setVisibility(View.VISIBLE);
            }else {
                btnSubmit.setVisibility(View.GONE);
            }
        } else {
            isOnline = getIntent().getBooleanExtra("isOnLine", false);
            if (1 == Constant.IS_ON_LINE){
                tvTitle.setText("地质地貌调查观测点-点详情");
                uuid = getIntent().getStringExtra("uuid");
                tabTitleBuilder.setEnabled();
                setEnableFalse();
                getDetailHttp();
                btnSubmit.setVisibility(View.GONE);
            }else {
                tvTitle.setText("地质地貌调查观测点-点");
                tvRightText.setText("保存");
                uuid = getIntent().getStringExtra("uuid");
                if (getIntent().hasExtra("_id")) {
                    _id = getIntent().getLongExtra("_id", -1);
                    setLocalData(_id);
                }
                btnSubmit.setVisibility(View.GONE);
            }

        }
    }

    public void setLocalData(long a) {
        if (a == -1) {
            return;
        }
        List<GeoGeomorphySvyPointDbEntity> students = daoSession.loadAll(GeoGeomorphySvyPointDbEntity.class);
        for (GeoGeomorphySvyPointDbEntity geologicalSvyPlanningLineDbEntity : students) {
            if (a == geologicalSvyPlanningLineDbEntity.get_id()) {
                setDetail(geologicalSvyPlanningLineDbEntity);
            }
        }
    }

    private void setEnableFalse() {
        etObjectCode.setEnabled(false);
        etGeologicalsvypointid.setEnabled(false);
        etFieldid.setEnabled(false);
        etGeomorphycode.setEnabled(false);
        etGeomorphyname.setEnabled(false);
        etGeomorphytype.setEnabled(false);
        etVerticaldisplacement.setEnabled(false);
        etHorizenoffset.setEnabled(false);
        etGeomorphylndirection.setEnabled(false);
        tvCreatedate.setEnabled(false);
        tvIssurfacerupturebelt.setEnabled(false);
        tvFracturetype.setEnabled(false);
        etPhotoAiid.setEnabled(false);
        tvPhotoviewingto.setEnabled(false);
        etPhotographer.setEnabled(false);
        etLength.setEnabled(false);
        tvWidth.setEnabled(false);
        etHeight.setEnabled(false);
        etTensiondisplacement.setEnabled(false);
        etFeature.setEnabled(false);
        tvIsmodifyworkmap.setEnabled(false);
        tvIsinmap.setEnabled(false);
        etLastangle.setEnabled(false);
        etCommentInfo.setEnabled(false);
    }
    private Gson gson = new Gson();;
    @OnClick({R.id.iv_back, R.id.tv_right_text, R.id.tv_createdate, R.id.tv_issurfacerupturebelt
            , R.id.tv_fracturetype, R.id.iv_geomorphylndirection, R.id.tv_photoviewingto
            , R.id.tv_ismodifyworkmap, R.id.tv_isinmap, R.id.tv_visibile, R.id.btn_submit
    ,R.id.iv_id_hint})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_isinmap:
                ShowBottomDialog.BottomDialog(this, new ShowBottomDialog.IsSlect() {
                    @Override
                    public void select(boolean b, Dialog dialog) {
                        if (b) {
                            tvIsinmap.setText("是");
                        } else {
                            tvIsinmap.setText("否");
                        }
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_ismodifyworkmap:
                ShowBottomDialog.BottomDialog(this, new ShowBottomDialog.IsSlect() {
                    @Override
                    public void select(boolean b, Dialog dialog) {
                        if (b) {
                            tvIsmodifyworkmap.setText("是");
                        } else {
                            tvIsmodifyworkmap.setText("否");
                        }
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_photoviewingto:
//                if (myDialog == null) {
//                    myDialog = DialogUtils.showRefreshDialog(activity);
//                } else {
//                    myDialog.show();
//                }
//                getHttpData("CVD-16-Direction");
                FaultTypeBean faultTypeBean3 = gson.fromJson(JsonBean.CVD_16_Direction, FaultTypeBean.class);
                setFaultTypeCVD(faultTypeBean3, "CVD-16-Direction");
                break;
            case R.id.iv_geomorphylndirection:
                HYDialogUtils.showCommonAlertDialog(this, "角度取值范围0~360"
                        , new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                            }
                        });
                break;
            case R.id.tv_visibile:
                if (llVisibile.getVisibility() == View.VISIBLE) {
                    llVisibile.setVisibility(View.GONE);
                    tvVisibile.setText("显示更多");
                } else {
                    llVisibile.setVisibility(View.VISIBLE);
                    tvVisibile.setText("隐藏更多");
                }
                break;
            case R.id.tv_createdate:
//                if (myDialog == null) {
//                    myDialog = DialogUtils.showRefreshDialog(activity);
//                } else {
//                    myDialog.show();
//                }
//                getHttpData("AgeCVD");
                FaultTypeBean faultTypeBean1 = gson.fromJson(JsonBean.AgeCVD, FaultTypeBean.class);
                setFaultTypeCVD(faultTypeBean1, "AgeCVD");
                break;
            case R.id.tv_issurfacerupturebelt:
                ShowBottomDialog.BottomDialog(this, new ShowBottomDialog.IsSlect() {
                    @Override
                    public void select(boolean b, Dialog dialog) {
                        if (b) {
                            tvIssurfacerupturebelt.setText("是");
                        } else {
                            tvIssurfacerupturebelt.setText("否");
                        }
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_fracturetype:
//                if (myDialog == null) {
//                    myDialog = DialogUtils.showRefreshDialog(activity);
//                } else {
//                    myDialog.show();
//                }
//                getHttpData("FracPtTypeCVD");
                FaultTypeBean faultTypeBean2 = gson.fromJson(JsonBean.FracPtTypeCVD, FaultTypeBean.class);
                setFaultTypeCVD(faultTypeBean2, "FracPtTypeCVD");
                break;
            case R.id.tv_right_text:
                //TODO 调用保存表单接口
                save();
                break;
            case R.id.btn_submit:
                submit();
                break;
            case R.id.iv_id_hint:
                HYDialogUtils.showCommonAlertDialog(this, "长度17位，前9位，行政区划代码6位+3位专题号，后8位自定义编码，字母数字均可，不足8位#补齐\n" +
                                "3位专题号，用户自己设置。一般就是100，101，102...,201,202"
                        , new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                            }
                        });
                break;
        }
    }

    public void save() {
        GeoGeomorphySvyPointDbEntity geologicalSvyPlanningLineDbEntity = new GeoGeomorphySvyPointDbEntity();

        String province = tabTitleBuilder.getProvinceStr();
        String city = tabTitleBuilder.getCityStr();
        String area = tabTitleBuilder.getAreaStr();
        String lat = tabTitleBuilder.getLatStr();
        String lon = tabTitleBuilder.getLonStr();
        String buildingAddress = tabTitleBuilder.getBuilderAdressStr();
        String objectCode = etObjectCode.getText().toString().trim();
        String geologicalsvypointid = etGeologicalsvypointid.getText().toString().trim();
        String fieldid = etFieldid.getText().toString().trim();
        String geomorphycode = etGeomorphycode.getText().toString().trim();
        String geomorphyname = etGeomorphyname.getText().toString().trim();
        String geomorphytype = etGeomorphytype.getText().toString().trim();
        String verticaldisplacement = etVerticaldisplacement.getText().toString().trim();
        String horizenoffset = etHorizenoffset.getText().toString().trim();
        String geomorphylndirection = etGeomorphylndirection.getText().toString().trim();
        String createdate = tvCreatedate.getText().toString().trim();
        String issurfacerupturebelt = tvIssurfacerupturebelt.getText().toString().trim();
        String fracturetype = tvFracturetype.getText().toString().trim();
        String photoAiid = etPhotoAiid.getText().toString().trim();
        String photoviewingto = tvPhotoviewingto.getText().toString().trim();
        String photographer = etPhotographer.getText().toString().trim();
        String length = etLength.getText().toString().trim();
        String width = tvWidth.getText().toString().trim();
        String height = etHeight.getText().toString().trim();
        String tensiondisplacement = etTensiondisplacement.getText().toString().trim();
        String feature = etFeature.getText().toString().trim();
        String ismodifyworkmap = tvIsmodifyworkmap.getText().toString().trim();
        String isinmap = tvIsinmap.getText().toString().trim();
        String lastangle = etLastangle.getText().toString().trim();
        String commentInfo = etCommentInfo.getText().toString().trim();
        if (TextUtils.isEmpty(province)) {
            Toast("请录入地区");
            return;
        }
        if (TextUtils.isEmpty(city)) {
            Toast("请录入地区");
            return;
        }
        if (TextUtils.isEmpty(area)) {
            Toast("请录入地区");
            return;
        }
        if (TextUtils.isEmpty(lat)) {
            Toast("请录入经度");
            return;
        }
        if (TextUtils.isEmpty(lon)) {
            Toast("请录入纬度");
            return;
        }
        if (TextUtils.isEmpty(buildingAddress)) {
            Toast("请录入详细地址");
            return;
        }
        if (TextUtils.isEmpty(objectCode)) {
            Toast("请录入编号");
            return;
        }
        if (objectCode.length() != 17){
            Toast("请录入正确的编号");
            return;
        }
        if (TextUtils.isEmpty(fieldid)) {
            Toast("请录入观测点野外编号");
            return;
        }
        if (!TextUtils.isEmpty(geomorphylndirection)) {
            double d2 = Double.parseDouble(geomorphylndirection);
            if (d2 < 0 || d2 > 360) {
                Toast("断层倾向角度值范围 0 ~ 360");
                return;
            }
        }
        geologicalSvyPlanningLineDbEntity.setProvince(province);
        geologicalSvyPlanningLineDbEntity.setCity(city);
        geologicalSvyPlanningLineDbEntity.setArea(area);
        geologicalSvyPlanningLineDbEntity.setLat(lat);
        geologicalSvyPlanningLineDbEntity.setLon(lon);
        geologicalSvyPlanningLineDbEntity.setTown(buildingAddress);
        geologicalSvyPlanningLineDbEntity.setId(objectCode);
        geologicalSvyPlanningLineDbEntity.setGeologicalsvypointid(geologicalsvypointid);
        geologicalSvyPlanningLineDbEntity.setFieldid(fieldid);
        geologicalSvyPlanningLineDbEntity.setGeomorphycode(geomorphycode);
        geologicalSvyPlanningLineDbEntity.setGeomorphyname(geomorphyname);
        geologicalSvyPlanningLineDbEntity.setGeomorphytype(geomorphytype);
        geologicalSvyPlanningLineDbEntity.setVerticaldisplacement(verticaldisplacement);
        geologicalSvyPlanningLineDbEntity.setHorizenoffset(horizenoffset);
        geologicalSvyPlanningLineDbEntity.setGeomorphylndirection(geomorphylndirection);
        geologicalSvyPlanningLineDbEntity.setCreatedate(null == ageCVDBean ? null == geoGeomorphySvyPointDbEntity ? "":geoGeomorphySvyPointDbEntity.getCreatedate() : ageCVDBean.getDictItemCode());
        geologicalSvyPlanningLineDbEntity.setCreatedateName(createdate);
        geologicalSvyPlanningLineDbEntity.setIssurfacerupturebelt("是".equals(issurfacerupturebelt) ? "1" : "0");
        geologicalSvyPlanningLineDbEntity.setFracturetype(null == fracPtTypeCVDBeab ? null == geoGeomorphySvyPointDbEntity ? "":geoGeomorphySvyPointDbEntity.getFracturetype() : fracPtTypeCVDBeab.getDictItemCode());
        geologicalSvyPlanningLineDbEntity.setFracturetypeName(fracturetype);
        geologicalSvyPlanningLineDbEntity.setPhotoAiid(photoAiid);
        geologicalSvyPlanningLineDbEntity.setPhotoviewingto(null == DirectionBean ? null == geoGeomorphySvyPointDbEntity ? "":geoGeomorphySvyPointDbEntity.getPhotoviewingto() : DirectionBean.getDictItemCode());
        geologicalSvyPlanningLineDbEntity.setPhotoviewingtoName(photoviewingto);
        geologicalSvyPlanningLineDbEntity.setPhotographer(photographer);
        geologicalSvyPlanningLineDbEntity.setLength(length);
        geologicalSvyPlanningLineDbEntity.setWidth(width);
        geologicalSvyPlanningLineDbEntity.setHeight(height);
        geologicalSvyPlanningLineDbEntity.setTensiondisplacement(tensiondisplacement);
        geologicalSvyPlanningLineDbEntity.setFeature(feature);
        geologicalSvyPlanningLineDbEntity.setIsmodifyworkmap("是".equals(ismodifyworkmap) ? "1" : "0");
        geologicalSvyPlanningLineDbEntity.setIsinmap("是".equals(isinmap) ? "1" : "0");
        geologicalSvyPlanningLineDbEntity.setLastangle(lastangle);
        geologicalSvyPlanningLineDbEntity.setCommentInfo(commentInfo);
        if (Constant.IS_ON_LINE == 1) {
            geologicalSvyPlanningLineDbEntity.setTaskId(taskId);
            geologicalSvyPlanningLineDbEntity.setProjectId(projectId);
            geologicalSvyPlanningLineDbEntity.setProjectName(projectName);
            geologicalSvyPlanningLineDbEntity.setTaskName(taskName);
        }
        if (-1 != _id) {
            geologicalSvyPlanningLineDbEntity.set_id(_id);
        }
        daoSession.insertOrReplace(geologicalSvyPlanningLineDbEntity);

        CustomToast.showToast(this, "保存成功");
        finish();
    }

    public void submit() {
        String province = tabTitleBuilder.getProvinceStr();
        String city = tabTitleBuilder.getCityStr();
        String area = tabTitleBuilder.getAreaStr();
        String lat = tabTitleBuilder.getLatStr();
        String lon = tabTitleBuilder.getLonStr();
        String buildingAddress = tabTitleBuilder.getBuilderAdressStr();
        String objectCode = etObjectCode.getText().toString().trim();
        String geologicalsvypointid = etGeologicalsvypointid.getText().toString().trim();
        String fieldid = etFieldid.getText().toString().trim();
        String geomorphycode = etGeomorphycode.getText().toString().trim();
        String geomorphyname = etGeomorphyname.getText().toString().trim();
        String geomorphytype = etGeomorphytype.getText().toString().trim();
        String verticaldisplacement = etVerticaldisplacement.getText().toString().trim();
        String horizenoffset = etHorizenoffset.getText().toString().trim();
        String geomorphylndirection = etGeomorphylndirection.getText().toString().trim();
        String createdate = tvCreatedate.getText().toString().trim();
        String issurfacerupturebelt = tvIssurfacerupturebelt.getText().toString().trim();
        String fracturetype = tvFracturetype.getText().toString().trim();
        String photoAiid = etPhotoAiid.getText().toString().trim();
        String photoviewingto = tvPhotoviewingto.getText().toString().trim();
        String photographer = etPhotographer.getText().toString().trim();
        String length = etLength.getText().toString().trim();
        String width = tvWidth.getText().toString().trim();
        String height = etHeight.getText().toString().trim();
        String tensiondisplacement = etTensiondisplacement.getText().toString().trim();
        String feature = etFeature.getText().toString().trim();
        String ismodifyworkmap = tvIsmodifyworkmap.getText().toString().trim();
        String isinmap = tvIsinmap.getText().toString().trim();
        String lastangle = etLastangle.getText().toString().trim();
        String commentInfo = etCommentInfo.getText().toString().trim();

        if (TextUtils.isEmpty(province)) {
            Toast("请录入地区");
            return;
        }
        if (TextUtils.isEmpty(city)) {
            Toast("请录入地区");
            return;
        }
        if (TextUtils.isEmpty(area)) {
            Toast("请录入地区");
            return;
        }
        if (TextUtils.isEmpty(lat)) {
            Toast("请录入经度");
            return;
        }
        if (TextUtils.isEmpty(lon)) {
            Toast("请录入纬度");
            return;
        }
        if (TextUtils.isEmpty(buildingAddress)) {
            Toast("请录入详细地址");
            return;
        }
        if (TextUtils.isEmpty(objectCode)) {
            Toast("请录入编号");
            return;
        }
        if (objectCode.length() != 17){
            Toast("请录入正确的编号");
            return;
        }
        if (TextUtils.isEmpty(fieldid)) {
            Toast("请录入观测点野外编号");
            return;
        }
        if (!TextUtils.isEmpty(geomorphylndirection)) {
            double d2 = Double.parseDouble(geomorphylndirection);
            if (d2 < 0 || d2 > 360) {
                Toast("断层倾向角度值范围 0 ~ 360");
                return;
            }
        }

        if (myDialog == null) {
            myDialog = DialogUtils.showRefreshDialog(activity);
        } else {
            myDialog.show();
        }

        DialogUtils.showIOSDialog(activity, "提示", "", "", "", ""
                , new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != myDialog)
                            myDialog.dismiss();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        photoRecycleBuild.submitImage(photoRecycleBuild.getSrcList(), 1, new PhotoRecycleBuild.ImageCallBack() {
                            @Override
                            public void onCallBack(String url) {
                                GeoGeomorphySvyPointActivity.this.urlArr = url;

                                JsonBuilder jsonBuilder = new JsonBuilder(GeoGeomorphySvyPointActivity.this);
                                jsonBuilder.put("province", province);
                                jsonBuilder.put("city", city);
                                jsonBuilder.put("area", area);
                                jsonBuilder.put("lat", lon);
                                jsonBuilder.put("lon", lat);
                                jsonBuilder.put("town", buildingAddress);
                                jsonBuilder.put("type", type);
                                jsonBuilder.put("taskId", taskId);
                                jsonBuilder.put("projectId", projectId);
                                jsonBuilder.put("projectName", projectName);
                                jsonBuilder.put("taskName", taskName);
                                jsonBuilder.put("userId", SPUtil.get("userId", ""));
                                jsonBuilder.put("id", objectCode);
                                jsonBuilder.put("geologicalsvypointid", geologicalsvypointid);
                                jsonBuilder.put("fieldid", fieldid);
                                jsonBuilder.put("geomorphycode", geomorphycode);
                                jsonBuilder.put("geomorphyname", geomorphyname);
                                jsonBuilder.put("geomorphytype", geomorphytype);
                                jsonBuilder.put("verticaldisplacement", verticaldisplacement);
                                jsonBuilder.put("horizenoffset", horizenoffset);
                                jsonBuilder.put("geomorphylndirection", geomorphylndirection);
                                jsonBuilder.put("issurfacerupturebelt", "是".equals(issurfacerupturebelt) ? 1 : 0);
                                jsonBuilder.put("photoAiid", photoAiid);
                                jsonBuilder.put("photographer", photographer);
                                jsonBuilder.put("length", length);
                                jsonBuilder.put("width", width);
                                jsonBuilder.put("height", height);
                                jsonBuilder.put("tensiondisplacement", tensiondisplacement);
                                jsonBuilder.put("feature", feature);
                                jsonBuilder.put("ismodifyworkmap", "是".equals(ismodifyworkmap) ? 1 : 0);
                                jsonBuilder.put("isinmap", "是".equals(isinmap) ? 1 : 0);
                                jsonBuilder.put("lastangle", lastangle);
                                jsonBuilder.put("commentInfo", commentInfo);
                                jsonBuilder.put("extends7", urlArr);
                                jsonBuilder.put("createUser", SPUtil.get("userId", ""));
                                if (null == geoGeomorphySvyPointDbEntity){
                                    jsonBuilder.put("createdate", null == ageCVDBean ? "" : ageCVDBean.getDictItemCode());
                                    jsonBuilder.put("createdateName", null == ageCVDBean ? "" : ageCVDBean.getDictItemName());
                                    jsonBuilder.put("fracturetype", null == fracPtTypeCVDBeab ? "" : fracPtTypeCVDBeab.getDictItemCode());
                                    jsonBuilder.put("fracturetypeName", null == fracPtTypeCVDBeab ? "" : fracPtTypeCVDBeab.getDictItemName());
                                    jsonBuilder.put("photoviewingto", null == DirectionBean ? "" : DirectionBean.getDictItemCode());
                                    jsonBuilder.put("photoviewingtoName", null == DirectionBean ? "" : DirectionBean.getDictItemName());
                                }else {
                                    jsonBuilder.put("createdate", geoGeomorphySvyPointDbEntity.getCreatedate());
                                    jsonBuilder.put("createdateName",  geoGeomorphySvyPointDbEntity.getCreatedateName());
                                    jsonBuilder.put("fracturetype",  geoGeomorphySvyPointDbEntity.getFracturetype());
                                    jsonBuilder.put("fracturetypeName",  geoGeomorphySvyPointDbEntity.getFracturetypeName());
                                    jsonBuilder.put("photoviewingto", geoGeomorphySvyPointDbEntity.getPhotoviewingto());
                                    jsonBuilder.put("photoviewingtoName",geoGeomorphySvyPointDbEntity.getPhotoviewingtoName());
                                }
                                MyHttpClient
                                        .newBuilder()
                                        .post()
                                        .url(Api.DCGCD_DICT_CODE_SAVE)
                                        .addParam(jsonBuilder.toJsonString())
                                        .build()
                                        .enqueue(new BaseCallBack<LoginBean>() {
                                            @Override
                                            public void onSuccess(LoginBean bean) {
                                                super.onSuccess(bean);
                                                if (bean.getCode() == 200) {
                                                    if (-1 != _id && null != geoGeomorphySvyPointDbEntity) {
                                                        try {
                                                            daoSession.delete(geoGeomorphySvyPointDbEntity);
                                                        } catch (Exception e) {
                                                            e.printStackTrace();
                                                        }
                                                    }
                                                    CustomToast.showToast(GeoGeomorphySvyPointActivity.this, "提交成功");
                                                    finish();
                                                }
                                                if (myDialog != null)
                                                    myDialog.dismiss();
                                            }

                                            @Override
                                            public void onFailure(Call call, IOException e) {
                                                super.onFailure(call, e);
                                                if (myDialog != null)
                                                    myDialog.dismiss();
                                                CustomToast.showToast(GeoGeomorphySvyPointActivity.this, "网络异常");
                                            }

                                            @Override
                                            public void onError(int code) {
                                                super.onError(code);
                                                if (myDialog != null)
                                                    myDialog.dismiss();
                                                CustomToast.showToast(GeoGeomorphySvyPointActivity.this, "网络异常");
                                            }
                                        });
                            }
                        });
                    }
                }, "提交后不能再修改");

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case HYImageUtils.REQUEST_CODE_FROM_CAMERA:
                List<Uri> list_1 = new ArrayList<>();
                list_1.add(HYImageUtils.imageUriFromCamera);
                photoRecycleBuild.updataAdapter(list_1);
                break;
            case HYImageUtils.REQUEST_CODE_FROM_ALBUM:
                ArrayList<String> files = data.getStringArrayListExtra("files");
                if (files == null || files.size() == 0) {
                    return;
                }
                List<Uri> list_2 = new ArrayList<>();
                for (String file : files) {
                    list_2.add(Uri.fromFile(new File(file)));
                }
                photoRecycleBuild.updataAdapter(list_2);
                // submitImage(list_2, correctTypeListEntity.getFieldList().get(lastClickItemPosition));
                break;
        }
    }

    private void getHttpData(String type) {
        MyHttpClient
                .newBuilder()
                .get()
                .url(Api.DCGCD_DICT_CODE + type)
                .build()
                .enqueue(new BaseCallBack<FaultTypeBean>() {
                    @Override
                    public void onSuccess(FaultTypeBean bean) {
                        super.onSuccess(bean);
                        if (bean.getCode() == 200) {
                            setFaultTypeCVD(bean, type);
                        }
                        if (myDialog != null)
                            myDialog.dismiss();
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(GeoGeomorphySvyPointActivity.this, "网络异常");
                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(GeoGeomorphySvyPointActivity.this, "网络异常");
                    }
                });
    }

    //断层性质数据
    private List<FaultTypeBean.DataBean> dataBeans = new ArrayList<>();
    private FaultTypeBean.DataBean ageCVDBean;
    private FaultTypeBean.DataBean DirectionBean;
    private FaultTypeBean.DataBean fracPtTypeCVDBeab;

    public void setFaultTypeCVD(FaultTypeBean bean, String type) {
        dataBeans.clear();
        dataBeans.addAll(bean.getData());
        String[] arr = new String[dataBeans.size()];
        for (int i = 0; i < dataBeans.size(); i++) {
            arr[i] = dataBeans.get(i).getDictItemName();
        }
        HYDialogUtils.showSingleChoiceItemAlertDialog(this, "选择", 0, arr
                , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        switch (type) {
//                            case "DirectionInDegreeRD":
//                                dataBean = dataBeans.get(i);
//                                tvFeature.setText(dataBeans.get(i).getDictItemName());
//                                break;
                            case "AgeCVD":
                                ageCVDBean = dataBeans.get(i);
                                tvCreatedate.setText(dataBeans.get(i).getDictItemName());
                                break;
                            case "FracPtTypeCVD":
                                fracPtTypeCVDBeab = dataBeans.get(i);
                                tvFracturetype.setText(dataBeans.get(i).getDictItemName());
                                break;
                            case "CVD-16-Direction":
                                DirectionBean = dataBeans.get(i);
                                tvPhotoviewingto.setText(dataBeans.get(i).getDictItemName());
                                break;
                        }
                        dialogInterface.dismiss();
                    }
                });
    }

    public void getDetailHttp() {
        if (myDialog == null) {
            myDialog = DialogUtils.showRefreshDialog(activity);
        } else {
            myDialog.show();
        }
        String url = Api.DCGCD_DICT_CODE_SAVE + "/" + uuid;
        MyHttpClient
                .newBuilder()
                .get()
                .url(url)
                .build()
                .enqueue(new BaseCallBack<GeoGeomorphySvyPointEntity>() {
                    @Override
                    public void onSuccess(GeoGeomorphySvyPointEntity bean) {
                        super.onSuccess(bean);
                        if (bean.getCode() == 200) {
                            setDetailData(bean);
                        }
                        if (myDialog != null)
                            myDialog.dismiss();
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(GeoGeomorphySvyPointActivity.this, "网络异常");
                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(GeoGeomorphySvyPointActivity.this, "网络异常");
                    }
                });
    }

    private void setDetailData(GeoGeomorphySvyPointEntity bean) {
        tabTitleBuilder.setDetailText(bean.getData().getProvince(), bean.getData().getCity()
                , bean.getData().getArea(), bean.getData().getLat(), bean.getData().getLon(), bean.getData().getTown());

        etObjectCode.setText(StringUtil.isNull(bean.getData().getId()));
        etGeologicalsvypointid.setText(StringUtil.isNull(bean.getData().getGeologicalsvypointid()));
        etFieldid.setText(StringUtil.isNull(bean.getData().getFieldid()));
        etGeomorphycode.setText(StringUtil.isNull(bean.getData().getGeomorphycode()));
        etGeomorphyname.setText(StringUtil.isNull(bean.getData().getGeomorphyname()));
        etGeomorphytype.setText(StringUtil.isNull(bean.getData().getGeomorphytype()));
        etVerticaldisplacement.setText(StringUtil.isNull(bean.getData().getVerticaldisplacement()));
        etHorizenoffset.setText(StringUtil.isNull(bean.getData().getHorizenoffset()));
        etGeomorphylndirection.setText(StringUtil.isNull(bean.getData().getGeomorphylndirection()));
        tvCreatedate.setText(StringUtil.isNull(bean.getData().getCreatedateName()));
        tvIssurfacerupturebelt.setText("1".equals(bean.getData().getIssurfacerupturebelt()) ? "是" : "否");
        tvFracturetype.setText(StringUtil.isNull(bean.getData().getFracturetypeName()));
        etPhotoAiid.setText(StringUtil.isNull(bean.getData().getPhotoAiid()));
        tvPhotoviewingto.setText(StringUtil.isNull(bean.getData().getPhotoviewingtoName()));
        etPhotographer.setText(StringUtil.isNull(bean.getData().getPhotographer()));
        etLength.setText(StringUtil.isNull(bean.getData().getLength()));
        tvWidth.setText(StringUtil.isNull(bean.getData().getWidth()));
        etHeight.setText(StringUtil.isNull(bean.getData().getHeight()));
        etTensiondisplacement.setText(StringUtil.isNull(bean.getData().getTensiondisplacement()));
        etFeature.setText(StringUtil.isNull(bean.getData().getFeature()));
        tvIsmodifyworkmap.setText("1".equals(bean.getData().getIsmodifyworkmap()) ? "是" : "否");
        tvIsinmap.setText("1".equals(bean.getData().getIsinmap()) ? "是" : "否");
        etLastangle.setText(StringUtil.isNull(bean.getData().getLastangle()));
        etCommentInfo.setText(StringUtil.isNull(bean.getData().getCommentInfo()));
        photoRecycleBuild.setHttpUrl(bean.getData().getExtends7());
    }

    private GeoGeomorphySvyPointDbEntity geoGeomorphySvyPointDbEntity;

    private void setDetail(GeoGeomorphySvyPointDbEntity bean) {
        this.geoGeomorphySvyPointDbEntity = bean;
        tabTitleBuilder.setDetailText(bean.getProvince(), bean.getCity()
                , bean.getArea(), bean.getLat(), bean.getLon(), bean.getTown());

        etObjectCode.setText(StringUtil.isNull(bean.getId()));
        etGeologicalsvypointid.setText(StringUtil.isNull(bean.getGeologicalsvypointid()));
        etFieldid.setText(StringUtil.isNull(bean.getFieldid()));
        etGeomorphycode.setText(StringUtil.isNull(bean.getGeomorphycode()));
        etGeomorphyname.setText(StringUtil.isNull(bean.getGeomorphyname()));
        etGeomorphytype.setText(StringUtil.isNull(bean.getGeomorphytype()));
        etVerticaldisplacement.setText(StringUtil.isNull(bean.getVerticaldisplacement()));
        etHorizenoffset.setText(StringUtil.isNull(bean.getHorizenoffset()));
        etGeomorphylndirection.setText(StringUtil.isNull(bean.getGeomorphylndirection()));
        tvCreatedate.setText(StringUtil.isNull(bean.getCreatedateName()));
        tvIssurfacerupturebelt.setText("1".equals(bean.getIssurfacerupturebelt()) ? "是" : "否");
        tvFracturetype.setText(StringUtil.isNull(bean.getFracturetypeName()));
        etPhotoAiid.setText(StringUtil.isNull(bean.getPhotoAiid()));
        tvPhotoviewingto.setText(StringUtil.isNull(bean.getPhotoviewingtoName()));
        etPhotographer.setText(StringUtil.isNull(bean.getPhotographer()));
        etLength.setText(StringUtil.isNull(bean.getLength()));
        tvWidth.setText(StringUtil.isNull(bean.getWidth()));
        etHeight.setText(StringUtil.isNull(bean.getHeight()));
        etTensiondisplacement.setText(StringUtil.isNull(bean.getTensiondisplacement()));
        etFeature.setText(StringUtil.isNull(bean.getFeature()));
        tvIsmodifyworkmap.setText("1".equals(bean.getIsmodifyworkmap()) ? "是" : "否");
        tvIsinmap.setText("1".equals(bean.getIsinmap()) ? "是" : "否");
        etLastangle.setText(StringUtil.isNull(bean.getLastangle()));
        etCommentInfo.setText(StringUtil.isNull(bean.getCommentInfo()));
        if (!TextUtils.isEmpty(bean.getExtends7())) {
            String[] arr = bean.getExtends7().split(",");
            List<Uri> list = new ArrayList<>();
            for (int i = 0; i < arr.length; i++) {
                File file = new File(arr[i]);
                Uri uri = Uri.fromFile(file);
                list.add(uri);
            }
            photoRecycleBuild.updataAdapter(list);
        }
    }
}
